package guoanmaker.operator.business.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.aliyun.oss.OSSClient;

import guoanmaker.operator.business.model.OperatorActivityEnlist;
import guoanmaker.operator.business.model.OperatorEventDetails;
import guoanmaker.operator.business.modelnobind.operatorEnlistDetails;
import guoanmaker.operator.business.service.OperatorActivityEnlistServiceInterface;
import guoanmaker.operator.helper.common.AjaxJson;
import guoanmaker.operator.helper.common.abstractobj.ApiController;
import guoanmaker.operator.helper.common.util.OSSUnitHelper;
import io.swagger.annotations.ApiOperation;

/**
 *  
 *
 * @author wanghongwei 
 * @ClassName: OperatorActivityEnlistController
 * @Description: OperatorActivityEnlistController 
 * @date 2017/8/11 下午16:38
 */
@RestController
@RequestMapping(ApiController.OPERATOR_ACTIVITY_ENLIST)
public class OperatorActivityEnlistController {
	private final AjaxJson ajaxJson;
	private final  OperatorActivityEnlistServiceInterface operatorActivityEnlistServiceInterface;
	public OperatorActivityEnlistController(AjaxJson ajaxJson,OperatorActivityEnlistServiceInterface operatorActivityEnlistServiceInterface){
		 this.ajaxJson = ajaxJson;
		 this.operatorActivityEnlistServiceInterface=operatorActivityEnlistServiceInterface;
	 }
	 /** 
	    * @Title: saveActivityEnlist
	    * @Description: 保存报名列表信息
	    * @param: OperatorActivityEnlist  
	    * @return AjaxJson    
	    * @throws 
	    */
		
	    @ApiOperation(value = "保存报名表单信息", notes = "保存报名表单信息")
	    @PostMapping(value = "saveActivityEnlist")
	    public AjaxJson saveActivityEnlist(@RequestBody OperatorActivityEnlist operatorActivityEnlist) {
	       OperatorActivityEnlist ActivityEnlist=operatorActivityEnlistServiceInterface.saveActivityEnlist(operatorActivityEnlist);
	       if(ActivityEnlist == null){
	    	return  ajaxJson.setSuccess(false).setStatus(103).setMsg("与活动日期不匹配").setData(null);
	       } 
	       if(ActivityEnlist.getId().equals("-101")){
	    	return  ajaxJson.setSuccess(false).setStatus(101).setMsg("活动报名人数已满").setData(null);
	       }
	       if(ActivityEnlist.getId().equals("-102")){
		    	return  ajaxJson.setSuccess(false).setStatus(102).setMsg("支付金额与后台运算不符").setData(null);
		    }
	       if(ActivityEnlist.getId().equals("-104")){
		    	return  ajaxJson.setSuccess(false).setStatus(104).setMsg("达到最大购买限制").setData(null);
		    }
	       if(ActivityEnlist.getId().equals("-105")){
		    	return  ajaxJson.setSuccess(false).setStatus(200).setMsg("购买数量超过当前预售票数").setData(null);
		    }
	        return ajaxJson.setSuccess(true).setStatus(200).setData(ActivityEnlist);
	    }
	    /**
		 *  
		 * 
		 * @Title: queryActivityUrl
		 * @Description: 活动链接地址查询
		 * @param: queryActivityUrl   
		 * @return AjaxJson     @throws 
		 */
	    @ApiOperation(value = "活动链接地址查询", notes = "活动链接地址查询")
	    @PostMapping(value = "queryActivityUrl")
	    public AjaxJson queryActivityUrl(@RequestParam String userId, String activityId) {
	    	OperatorEventDetails operatorEventDetails=operatorActivityEnlistServiceInterface.queryActivityUrl(userId,activityId);
	    	 if(operatorEventDetails.getId().equals("-104")){
			    return  ajaxJson.setSuccess(false).setStatus(200).setMsg("达到最大购买限制").setData(null);
			    }
	    	    return ajaxJson.setSuccess(true).setStatus(200).setData(operatorEventDetails);
	    }
	    /** 
		  * @Title: saveActivityEnlist
		  * @Description: 查询
		  * @param: OperatorActivityEnlist  
		  * @return AjaxJson    
		  * @throws 
		  */
	    @ApiOperation(value = "根据 报名表id/ 查询报名表单信息", notes = "根据 报名表id/ 查询报名表单信息")
	    @PostMapping(value = "getActivityEnlist")
	    public AjaxJson getActivityEnlist(@RequestParam String Id) {
	        return ajaxJson.setSuccess(true).setStatus(200).setData(operatorActivityEnlistServiceInterface.getActivityEnlist(Id));
	    }
	    /**
	     * @Title: getFenYeEnlistAll
		 * @Description: 查询
		 * @param: OperatorActivityEnlist  
		 * @return AjaxJson    
		 * @throws 
	     */
	    @ApiOperation(value = "用户id分页 查询报名表单信息", notes = "用户id分页 查询报名表单信息")
	    @PostMapping(value = "getFenYeEnlistAll")
	    public AjaxJson getFenYeEnlistAll(@RequestParam String userId,
	    		@RequestParam(value = "page", defaultValue = "0") Integer page,
	            @RequestParam(value = "size", defaultValue = "10") Integer size) {
	    	List<operatorEnlistDetails>  ActivityEnlist=operatorActivityEnlistServiceInterface.getFenYeEnlistAll(userId,page,size);
	        return ajaxJson.setSuccess(true).setStatus(200).setData(ActivityEnlist);
	    }
	    /**
	     * @Title: getFenYeEnlistAll
		 * @Description: 查询 和 总条数
		 * @param: OperatorActivityEnlist  
		 * @return AjaxJson    
		 * @throws 
	     */
	    @ApiOperation(value = "用户id分页 查询报名订单 和总条数", notes = "用户id分页 查询报名订单 和总条数")
	    @PostMapping(value = "getFenYeEnlistAndCount")
	    public AjaxJson getFenYeEnlistAndCount(@RequestParam String userId,
	    		@RequestParam(value = "page", defaultValue = "0") Integer page,
	            @RequestParam(value = "size", defaultValue = "10") Integer size) {
	    	OperatorActivityEnlist  ActivityEnlist=operatorActivityEnlistServiceInterface.getFenYeEnlistAndCount(userId,page,size);
	        return ajaxJson.setSuccess(true).setStatus(200).setData(ActivityEnlist);
	    }
	    /** 
		  * @Title: saveActivityEnlist
		  * @Description: 删除表单
		  * @param: OperatorActivityApplication   
		  * @return AjaxJson
		  * @throws 
		  */
	    @ApiOperation(value = "根据 报名表id 删除报名表单信息", notes = "根据 报名表id 删除报名表单信息")
	    @PostMapping(value = "deleteActivityEnlistById/{id}")
	    public AjaxJson  deleteActivityEnlistById(@PathVariable(value = "id") String id){
	    	operatorActivityEnlistServiceInterface.deleteActivityEnlistById(id);
	    	return ajaxJson.setSuccess(true).setStatus(200).setData("数据删除成功!");
	    }

	    /** 
		  * @Title: updateActivityEnlist
		  * @Description: 修改报名订单状态
		  * @param: OperatorActivityEnlist   
		  * @return AjaxJson    
		  * @throws 
		  */
	    @ApiOperation(value = " 修改报名订单状态", notes = " 修改报名订单状态")
	    @PostMapping(value = "updateActivityEnlistState")
	    public AjaxJson  updateActivityEnlistState(@RequestBody OperatorActivityEnlist operatorActivityEnlist){
	    	try {
				int count=operatorActivityEnlistServiceInterface.updateActivityEnlistState(operatorActivityEnlist);
				if(count==104){
				return  ajaxJson.setSuccess(false).setStatus(104).setMsg("达到最大购买限制").setData(null);
				}
				return ajaxJson.setSuccess(true).setStatus(200).setData("数据修改成功!");
	    	} catch (Exception e) {
				e.printStackTrace();
				System.out.println(e);
			}
	    	return ajaxJson.setSuccess(false).setStatus(500).setData("数据修改失败!");
	    }
	    /** 
		  * @Title: validateActivityEnlistCoupons
		  * @Description: 验证优惠券
		  * @param: OperatorActivityEnlist   
		  * @return AjaxJson    
		  * @throws 
		  */
	    @ApiOperation(value = " 验证优惠券", notes = " 验证优惠券")
	    @PostMapping(value = "validateActivityEnlistCoupons")
	    public AjaxJson validateActivityEnlistCoupons(@RequestBody OperatorActivityEnlist operatorActivityEnlist){
	    	int count=operatorActivityEnlistServiceInterface.validateActivityEnlistCoupons(operatorActivityEnlist);
	        if(count ==0){
	    	   return ajaxJson.setSuccess(true).setStatus(200).setData("success");
	        }
	        if(count ==1 ){
	           return ajaxJson.setSuccess(true).setStatus(200).setData("success");
	        }
	           return ajaxJson.setSuccess(false).setStatus(200).setData("failure");
	    }
	    /** 
		  * @Title: saveStreamNum
		  * @Description: saveStreamNu
		  * @param: OperatorActivityEnlist   
		  * @return AjaxJson    
		  * @throws 
		  */
	    @ApiOperation(value = "添加流水号", notes = " 添加流水号")
	    @PostMapping(value = "saveStreamNum")
	    public AjaxJson  saveStreamNum(@RequestBody OperatorActivityEnlist operatorActivityEnlist){
	       Integer count= operatorActivityEnlistServiceInterface.saveStreamNum(operatorActivityEnlist);
	    	if(count==1){
	    		return ajaxJson.setSuccess(true).setStatus(200).setMsg("数据修改成功!").setData(null);
	    	}
	    	return ajaxJson.setSuccess(false).setStatus(101).setMsg("数据修改失败!").setData(null);
	    }
	    
	    /** 
		  * @Title: saveStreamNum
		  * @Description: 活动报名文件上传接口 
		  * @param: OperatorActivityEnlist   
		  * @return AjaxJson    
		  * @throws 
		  */
	    @ApiOperation(value = "活动报名文件上传接口 ", notes = "活动报名文件上传接口")
	    @PostMapping(value = "/UploadActivityEnlistFile")
		public AjaxJson UploadActivityEnlistFile(MultipartFile upfile,HttpServletResponse response) { 
			  if (upfile != null) {
					OSSClient client = OSSUnitHelper.getOSSClient();
					String FileName=new Date().getTime()+(int)((Math.random()*9+1)*100000)+upfile.getOriginalFilename().substring(upfile.getOriginalFilename().indexOf("."));
					System.out.println(FileName);
					OSSUnitHelper.uploadObjectOSS(client, upfile, ApiController.ACTIVITYENLISTFILE_URL,
							FileName);
					String url = OSSUnitHelper.getUrl(client, ApiController.ACTIVITYENLISTFILE_URL, FileName);
					url = url.replace("http://", "https://");
					if (url != null && !url.equals("")) {
						ajaxJson.setSuccess(true).setStatus(200).setData(url);
						response.setContentType(url);
					}
				}
				return ajaxJson;
		  }
	    /** 
		  * @Title:  paymentActivityEnlist
		  * @Description: 发起活动订单支付
		  * @param: paymentActivityEnlist   
		  * @return AjaxJson    
		  * @throws 
		  */
	    @ApiOperation(value = " 发起活动订单支付 ", notes = " 发起活动订单支付")
	    @PostMapping(value = "/paymentActivityEnlist")
		public AjaxJson paymentActivityEnlist(@RequestParam String id,String channel,String back_url) { 
			return  ajaxJson.setSuccess(true).setStatus(200).setData(operatorActivityEnlistServiceInterface.paymentActivityEnlist(id, channel, back_url));
		  }
	    
	/**
	 *  
	 * 
	 * @Title: updateEnlistPayState
	 * @Description: 活动订单 支付状态变更
	 * @param: updateEnlistPayState   
	 * @return AjaxJson     @throws 
	 */
	@ApiOperation(value = " 活动订单 支付状态变更 ", notes = " 活动订单 支付状态变更")
	@PostMapping(value = "/updateEnlistPayState")
	public AjaxJson updateEnlistPayState(@RequestParam String id, String streamNum) {
		Integer count = operatorActivityEnlistServiceInterface.updateEnlistPayState(id, streamNum);
		if (count == 1) {
			return ajaxJson.setSuccess(true).setStatus(200).setMsg("订单支付状态修改成功").setData(null);
		}
		return ajaxJson.setSuccess(false).setStatus(101).setMsg("订单支付状态修改失敗").setData(null);
	}
}
